/**
 * <p>Copyright: Copyright (c) 2014</p>
 * <p>Company: -- Co. Ltd</p>
 * <p>2014-5-5</p>
 * @author Jan.Zhang
 */
package com.xianl.busiplat.util;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import com.xianl.busiplat.po.User;


/** 
 * desc:
 * <p>author: Jan.Zhang </p>
 * date: 2014-5-5
 */
public class PlatBeanUtil {
	
	public static Map<String, Object> getPoValue(Object obj) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		if(obj == null){
			return params;
		}
		Class<?> clazz = obj.getClass();
		for(Field f: clazz.getDeclaredFields()) {
			if (Modifier.isStatic(f.getModifiers()) || Modifier.isFinal(f.getModifiers())) {  
                continue;  
            } 
			f.setAccessible(true);
			Object fieldValue = f.get(obj);
			String pType = f.getGenericType().toString();
			String pName = f.getName();
			if(fieldValue!= null && "int".equals(pType) && f.getInt(obj)==0 && !"id".equals(pName)){
				continue;
			}
			if(fieldValue != null){
				params.put(f.getName(), fieldValue);
			}
		}
		return params;
	}
	
	public static void main(String[] args) {
		User user = new User();
		user.setId(1);
		user.setUsername("jan");
		user.setRealname("test");
		try {
			Map<String, Object> params = getPoValue(user);
			for(String s : params.keySet()){
				System.out.println(s+" : "+params.get(s));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
